package com.njlime.cms.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.njlime.cms.entities.Community;
import com.njlime.cms.entities.CommunityComment;
import com.njlime.cms.entities.Label;
import com.njlime.cms.service.sys.*;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.core.web.validation.annotation.support.ValidateService;
import com.njlime.member.entiies.MemberComment;
import com.njlime.sys.entities.Unit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by   on 2017/1/13.
 */
@Controller
@RequestMapping(value = "/cms/community")
public class CommunityController extends BaseController {
    @Resource
    CommunityService communityService;
    @Resource
    CommunityCommentService communityCommentService;
    @Resource
    private LabelService labelService;
    @Resource
    private ChannelService channelService;
    @Resource
    private ColumnService columnService;

    //--------------------控制器-------------------------

    /**
     * 跳转贴吧管理页面
     * @return
     */
    @RequestMapping(value = "commManage")
    public ModelAndView infoManage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/community/index");
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(Integer.MAX_VALUE);
        modelAndView.addObject("labelList",labelService.list(pageBean, new Label()).getMapList());
        //频道栏目
        Unit rootUnit = getLoginUser().getRootUnit();
        if(rootUnit != null){
            modelAndView.addObject("channelList",channelService.getChannelList(rootUnit.getId(),2));
        }
        return modelAndView;
    }

    /**
     * 跳转贴吧编辑页面
     * @return
     */
    @RequestMapping(value = "editView")
    public ModelAndView editView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/community/edit");
        //标签
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(Integer.MAX_VALUE);
        modelAndView.addObject("labelList",labelService.list(pageBean, new Label()).getMapList());
        //资讯详情
        Community community = communityService.findOne(id);
        checkDataPermission(community.getUnit_id(), community.getCreator());
        modelAndView.addObject("info", community);
        //频道栏目
        Unit rootUnit = getLoginUser().getRootUnit();
        modelAndView.addObject("channelList",channelService.getChannelList(rootUnit.getId(),2));
        return modelAndView;
    }

    /**
     * 跳转贴吧新增页面
     * @return
     */
    @RequestMapping(value = "addView")
    public ModelAndView addView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/community/add");
        //标签
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(Integer.MAX_VALUE);
        modelAndView.addObject("labelList",labelService.list(pageBean, new Label()).getMapList());
        //频道栏目
        Unit rootUnit = getLoginUser().getRootUnit();
        modelAndView.addObject("channelList",channelService.getChannelList(rootUnit.getId(),2));
        return modelAndView;
    }

    /**
     * 跳转贴吧评论页面
     * @return
     */
    @RequestMapping(value = "commentView")
    public ModelAndView commentView(HttpServletRequest request, Integer id) {
        ModelAndView modelAndView = new ModelAndView("/sys/content/community/comment");
        Community community = communityService.findOne(id);
        checkDataPermission(community.getUnit_id(), community.getCreator());
        modelAndView.addObject("info", community);
        return modelAndView;
    }

    //------------------功能--------------------------
    
    
    /**
     * 社区详情
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ResponseBody
    public String info(int id, HttpServletRequest request) {
        Community community = communityService.findOne(id);
        checkDataPermission(community.getUnit_id(), community.getCreator());
        return toJsonSuccessData(communityService.findOne(id));
    }

    /**
     * 保存社区
     *
     * @param info
     * @param request
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public String save(Community info, HttpServletRequest request) {
        try{
            ValidateService.valid(info);
        }catch (Exception e){
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
        info.setCreator(getLoginUser().getId());
        info.setUnit_id(getLoginUserUnitId());
        if(info.getCommunity_type() == Community.CommunityType.NORMAL){
            if(StringUtil.isEmpty(info.getPic()) || JSONArray.parseArray(info.getPic()).size() < 1){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, "至少需要一张图片");
            }
        }else{
            if(StringUtil.isEmpty(info.getPk_pic1()) || StringUtil.isEmpty(info.getPk_pic2())
                    || StringUtil.isEmpty(info.getPk_content1()) || StringUtil.isEmpty(info.getPk_content2())){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, "两支队伍的图片及介绍均不能为空");
            }
        }
        Integer id = communityService.save(info);
        if (id > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "添加失败");
        }
    }

    /**
     * 编辑社区
     *
     * @param info
     * @param request
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(Community info, HttpServletRequest request) {
        Community community = communityService.findOne(info.getId());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        info.setUpdater(getLoginUser().getId());
        info.setUpdate_time(new Date());
        if(info.getCommunity_type() == Community.CommunityType.NORMAL){
            if(StringUtil.isEmpty(info.getPic()) || JSONArray.parseArray(info.getPic()).size() < 1){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, "至少需要一张图片");
            }
        }else{
            if(StringUtil.isEmpty(info.getPk_pic1()) || StringUtil.isEmpty(info.getPk_pic2())
                    || StringUtil.isEmpty(info.getPk_content1()) || StringUtil.isEmpty(info.getPk_content2())){
                return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, "两支队伍的图片及介绍均不能为空");
            }
        }
        int count = communityService.update(info);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "编辑失败");
        }

    }

    /**
     * 删除社区
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request, Integer id) {
        Community community = communityService.findOne(id);
        checkDataPermission(community.getUnit_id(), community.getCreator());
        int count = communityService.del(id);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "删除失败");
        }
    }

    /**
     * 评论开关
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "toggleComment", method = RequestMethod.POST)
    @ResponseBody
    public String toggleComment(HttpServletRequest request, Integer id) {
        Community community = communityService.findOne(id);
        checkDataPermission(community.getUnit_id(), community.getCreator());
        int count = communityService.toggleComment(id);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "操作失败");
        }
    }

    /**
     * 上下线
     *
     * @param request
     * @param info
     * @return
     */
    @RequestMapping(value = "togglePublish", method = RequestMethod.POST)
    @ResponseBody
    public String togglePublish(HttpServletRequest request, Community info) {
        Community community = communityService.findOne(info.getId());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        int count = communityService.togglePublish(info);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "操作失败");
        }
    }


    /**
     * 获取文章列表
     *
     * @param pageBean
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(PageBean pageBean, Community info, HttpServletRequest request) {
        return toJsonListData(communityService.list(pageBean, info, false));
    }

    /**
     * 文章评论详情单个获取
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "commentInfo", method = RequestMethod.GET)
    @ResponseBody
    public String commentInfo(int id) {
        CommunityComment comment = communityCommentService.findOne(id);
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        return toJsonSuccessData(communityCommentService.findOne(id));
    }

    /**
     * 回复
     *
     * @param reply
     * @return
     */
    @RequestMapping(value = "commentReply", method = RequestMethod.POST)
    @ResponseBody
    public String commentReply(String reply, HttpServletRequest request, CommunityComment info) {
        CommunityComment comment = communityCommentService.findOne(info.getId());
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        communityCommentService.reply(info.getId(), reply, getLoginUser().getId());
        return toJsonSuccessData();
    }

    /**
     * 评论置顶
     *
     * @param comment 评论
     * @return
     */
    @RequestMapping(value = "commentTop", method = RequestMethod.POST)
    @ResponseBody
    public String commentTop(CommunityComment comment) {
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        communityCommentService.top(comment.getId());
        return toJsonSuccessData();
    }

    /**
     * 删除 文章评论
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "commentDel", method = RequestMethod.POST)
    @ResponseBody
    public String commentDel(HttpServletRequest request, CommunityComment info) {
        Community community = communityService.findOne(info.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        int count = communityCommentService.del(info);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "删除失败");
        }
    }

    /**
     * 文章评论点赞数  +1
     *
     * @param id 文章id
     * @return
     */
    @RequestMapping(value = "commentFabulous", method = RequestMethod.POST)
    @ResponseBody
    public String commentFabulous(int id) {
        CommunityComment comment = communityCommentService.findOne(id);
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        communityCommentService.like(id);
        return toJsonSuccessData();
    }

    /**
     * 审核
     *
     * @param id 文章id
     * @return
     */
    @RequestMapping(value = "commentAudit", method = RequestMethod.POST)
    @ResponseBody
    public String auditComment(int id) {
        CommunityComment comment = communityCommentService.findOne(id);
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        MemberComment memberComment = new MemberComment();
        memberComment.setMember_id(comment.getUser_id());
        memberComment.setBusiness_id(comment.getCommunity_id());
        memberComment.setBusiness_type(GlobalValue.BusinessType.COMMUNITY);
        memberComment.setBusiness_title(community.getTitle());
        memberComment.setCreate_time(new Date());
        communityCommentService.auditComment(comment, memberComment);
        return toJsonSuccessData();
    }

    /**
     * 文章评论列表获取
     *
     * @param pageBean
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "commentList", method = RequestMethod.GET)
    public String list(PageBean pageBean, CommunityComment info, HttpServletRequest request) {
        Community community = communityService.findOne(info.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        return toJsonListData(communityCommentService.list(pageBean, info));
    }

    /**
     * 奖励取消奖励
     *
     * @param id 文章id
     * @return
     */
    @RequestMapping(value = "commentReward", method = RequestMethod.POST)
    @ResponseBody
    public String commentReward(int id) {
        CommunityComment comment = communityCommentService.findOne(id);
        Community community = communityService.findOne(comment.getCommunity_id());
        checkDataPermission(community.getUnit_id(), community.getCreator());
        communityCommentService.commentReward(id);
        return toJsonSuccessData();
    }


}
